<template>
  <el-dialog
      title="添加/修改设备"
      :visible.sync="visible"
      :id.sync="id"
      @opened="openDialog"
      :before-close="closeDialog"
      width="480px">
    <el-form label-width="80px">
      <el-form-item label="学年" prop="term.year" size="small">
        <el-input size="small" v-model="term.year"></el-input>
      </el-form-item>
      <el-form-item label="学期类型" prop="term.termType" size="small">
        <el-select size="small" v-model="term.termType">
          <el-option
              v-for="item in termType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="term.startDate" size="small">
        <el-input size="small" v-model="term.startDate"></el-input>
      </el-form-item>
      <el-form-item label="结束时间" prop="term.endDate" size="small">
        <el-input size="small" v-model="term.endDate"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="term.state" size="small">
        <el-select size="small" v-model="term.state">
          <el-option
              v-for="item in state"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate" v-if="id!=''">修改设备</el-button>
        <el-button type="primary" size="small" plain @click="onSubmit" v-else>添加设备</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getTermById, updateTerm, createTerm} from "@/api/term";

export default {
  name: 'TermEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      state:[
        {id: 1, name: '未开始'},
        {id: 2, name: '进行中'},
        {id: 3, name: '已结束'},
      ],
      termType:[
        {id: 1, name: '春季学期'},
        {id: 2, name: '秋季学期'},
      ],
      term: {}
    }
  },
  methods: {

    // 添加设备信息
    onSubmit() {
      createTerm({
        'createTime': this.term.createTime,
        'endDate': this.term.endDate,
        'id': this.term.id,
        'startDate': this.term.startDate,
        'state': this.term.state,
        'termName': this.term.termName,
        'termType':this.term.termType,
        'username': this.term.username,
        'weekNumber': this.term.weekNumber,
        'year':this.term.year,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 更新设备信息
    handleUpdate() {
      updateTerm({
        'createTime': this.term.createTime,
        'endDate': this.term.endDate,
        'id': this.term.id,
        'startDate': this.term.startDate,
        'state': this.term.state,
        'termName': this.term.termName,
        'termType':this.term.termType,
        'username': this.term.username,
        'weekNumber': this.term.weekNumber,
        'year':this.term.year,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 打开对话框
    openDialog() {
      if (this.id !== '') {
        getTermById(this.id).then((response) => {
          this.term = response.data.data;
          delete this.term.createTime;
        })
      }
    },

    // 关闭对话框
    closeDialog() {
      this.term = {}
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>

</style>
